

//
document.getElementById('keyword').addEventListener('tap',function () {
    window.location.href="search.html"
})

document.getElementById('search-right').addEventListener('tap',function () {
    window.location.href="personal-news.html"
})

mui('body').on('tap','.more_brand',function () {
    window.location.href="brand.html?hrefId="+hrefId;
})
//

function doAjax() {
    $.ajax({
        url:'/goodsCategoryC/showGoodsCategory.json',
        dataType:'json',
        data:{
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function () {
            $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('#segmentedControlContents'));
        },
        success:function (data) {
            if(data.code==1){
                $('#segmentedControlContents').empty();
                var oneData = data.datas;
                for(var a=0;a<oneData.length;a++){
                    $('<a class="mui-control-item" data-index="'+(a)+'" href="#content'+(a+1)+'">'+oneData[a].name+'</a>').appendTo($('#segmentedControls'));

                    $('<div class="mui-control-content" id="content'+(a+1)+'">' +
                        '<div class="advertising_img" linkUrl="'+oneData[a].linkUrl+'">' +
                        '<img src="'+oneData[a].goodsImg+'" alt="" linkUrl="'+oneData[a].linkUrl+'">' +
                        '</div>' +
                        '<div class="mui-clearfix" id="brandBox'+oneData[a].goodsId+'"></div>'+
                        '<div class="mui-clearfix" id="recommendBox'+oneData[a].goodsId+'"></div>'+
                        '</div>').appendTo($('#segmentedControlContents'))
                    //热门品牌
                    var secondData = oneData[a].secondCategory;
                    $('<h2 class="category_small_title" style="margin-bottom: 6px;">' +
                        ''+secondData[0].name+'<span class="mui-icon mui-icon-arrowright"></span>' +
                        // '<span class="more_brand">更多品牌</span>' +
                        '</h2>' +
                        '<ul class="mui-table-view mui-clearfix">').appendTo($('#brandBox'+oneData[a].goodsId+''))
                    for(var b=0;b<secondData[0].thirdCategory.length;b++){
                        $('<li class="mui-table-view-cell">' +
                            '<img src="'+secondData[0].thirdCategory[b].imgUrl+'" alt="">' +
                            '<span>'+secondData[0].thirdCategory[b].name+'</span>'+
                            '</li></ul>').appendTo($('#brandBox'+oneData[a].goodsId+''))
                    }
                    //热门推荐
                    $('<h2 class="category_small_title">'+secondData[1].name+'</h2>' +
                        '<ul class="mui-table-view mui-clearfix">').appendTo($('#recommendBox'+oneData[a].goodsId+''))
                    for(var c=0;c<secondData[1].thirdCategory.length;c++){
                        $('<li class="mui-table-view-cell">' +
                            '<img src="'+secondData[1].thirdCategory[c].imgUrl+'" alt="">' +
                            '<span>'+secondData[1].thirdCategory[c].name+'</span>'+
                            '</li></ul>').appendTo($('#recommendBox'+oneData[a].goodsId+''))
                    }
                }
                doScroll();
            }
        }
    })
}

function doScroll() {
    mui('#segmentedControls').scroll({indicators:false});
    var controls = document.getElementById("segmentedControls");
    var contents = document.getElementById("segmentedControlContents");
    controls.querySelector('.mui-control-item').classList.add('mui-active');
    contents.querySelector('.mui-control-content').classList.add('mui-active');
    (function() {
        var controlsElem = document.getElementById("segmentedControls");
        var contentsElem = document.getElementById("segmentedControlContents");
        var controlListElem = controlsElem.querySelectorAll('.mui-control-item'); //返回所有左侧选项卡
        var contentListElem = contentsElem.querySelectorAll('.mui-control-content');//返回所有右侧对应content盒子
        var controlWrapperElem = controlsElem.parentNode; //返回左侧父节点mui-col-xs-3
        var controlWrapperHeight = controlWrapperElem.offsetHeight;//mui-col-xs-3高度
        var controlMaxScroll = controlWrapperElem.scrollHeight - controlWrapperHeight;//左侧类别最大可滚动高度
        var maxScroll = contentsElem.scrollHeight - contentsElem.offsetHeight;//右侧内容最大可滚动高度
        var controlHeight = controlListElem[0].offsetHeight;//左侧类别每一项的高度
        var controlTops = []; //存储control的scrollTop值
        var contentTops = [0]; //存储content的scrollTop值
        var length = contentListElem.length;

        for (var i = 0; i < length; i++) {
            controlTops.push(controlListElem[i].offsetTop + controlHeight);
        }
        for (var i = 1; i < length; i++) {
            var offsetTop = contentListElem[i].offsetTop;
            if (offsetTop + 100 >= maxScroll) {
                var height = Math.max(offsetTop + 100 - maxScroll, 100);
                var totalHeight = 0;
                var heights = [];
                for (var j = i; j < length; j++) {
                    var offsetHeight = contentListElem[j].offsetHeight;
                    totalHeight += offsetHeight;
                    heights.push(totalHeight);
                }
                for (var m = 0, len = heights.length; m < len; m++) {
                    contentTops.push(parseInt(maxScroll - (height - heights[m] / totalHeight * height)));
                }
                break;
            } else {
                contentTops.push(parseInt(offsetTop));
            }
        }
        contentsElem.addEventListener('scroll', function() {
            var scrollTop = contentsElem.scrollTop;
            for (var i = 0; i < length; i++) {
                var offsetTop = contentTops[i];
                var offset = Math.abs(offsetTop - scrollTop);
						//console.log("i:"+i+",scrollTop:"+scrollTop+",offsetTop:"+offsetTop+",offset:"+offset);
                if (scrollTop < offsetTop) {
                    if (scrollTop >= maxScroll) {
                        onScroll(length - 1);
                    } else {
                        onScroll(i);
                    }
                    break;
                } else if (offset < 20) {
                    onScroll(i);
                    break;
                }else if(scrollTop >= maxScroll){
                    onScroll(length - 1);
                    break;
                }
            }
        });
        var lastIndex = 0;
        //监听content滚动
        var onScroll = function(index) {
            if (lastIndex !== index) {
                lastIndex = index;
                var lastActiveElem = controlsElem.querySelector('.mui-active');
                lastActiveElem && (lastActiveElem.classList.remove('mui-active'));
                var currentElem = controlsElem.querySelector('.mui-control-item:nth-child(' + (index + 1) + ')');
                currentElem.classList.add('mui-active');
                //简单处理左侧分类滚动，要么滚动到底，要么滚动到顶
                var controlScrollTop = controlWrapperElem.scrollTop;
                if (controlScrollTop + controlWrapperHeight < controlTops[index]) {
                    controlWrapperElem.scrollTop = controlMaxScroll;
                } else if (controlScrollTop > controlTops[index] - controlHeight) {
                    controlWrapperElem.scrollTop = 0;
                }
            }
        };
        //滚动到指定content
        var scrollTo = function(index) {
            contentsElem.scrollTop = contentTops[index];
        };
        mui(controlsElem).on('tap', '.mui-control-item', function(e) {
            scrollTo(this.getAttribute('data-index'));
            var thisNum = this.getAttribute('data-index');
            //controlsElem.querySelector('.mui-control-item').classList.remove('mui-active');
            //controlsElem.querySelector('.mui-control-item:nth-child('+thisNum+')').classList.add('mui-active');
            return false;
        });
    })();
}

//doAjax();


var num = 0;
var parentCategoryId = '';
var hrefId = '';
//获取一级分类与二级分类
$(function () {
    function doLfet() {
        $.ajax({
            url:'/goodsCategoryC/oneLevelCategory.json',
            dataType:'json',
            data:{
                companyCode:getItem('companyCode'),
            },
            type:'post',
            success:function (data) {
                var oneData = data.datas;
                for(var i=0;i<oneData.length;i++){
                    $('<a class="mui-control-item" id="'+oneData[i].id+'" data-index="'+(i)+'" href="#content'+(i+1)+'">'+oneData[i].name+'</a>').appendTo($('#segmentedControls'));
                }
                var cateAct = getItem('cateAct');
                if(cateAct==null||cateAct==undefined||cateAct==""){
                    $('#segmentedControls .mui-control-item:first-child').addClass('mui-active');
                    parentCategoryId = $('#segmentedControls .mui-control-item:first-child').attr('id')
                }else{
                    $('#'+cateAct+'').addClass('mui-active');
                    parentCategoryId = $('#'+cateAct+'').attr('id')
                }

                doRight();
                removeItem('cateAct');
            }
        })
    }
    doLfet();
    function doRight() {
        $.ajax({
            url:'/goodsCategoryC/twoLevelCategory.json',
            dataType:'json',
            data:{
                parentCategoryId:parentCategoryId,
                companyCode:getItem('companyCode'),
            },
            type:'post',
            beforeSend:function () {
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('#segmentedControlContents'));
            },
            success:function (data) {
                $('#segmentedControlContents').empty();
                var secondData = data.datas;
                $('<div class="mui-control-content" id="content'+(num+1)+'">' +
                    '<div class="advertising_img" linkUrl="'+secondData.linkUrl+'">' +
                    '<img src="'+secondData.imgUrl+'" alt="">' +
                    '</div>' +
                    '<div class="mui-clearfix" id="brandBox'+num+'"></div>'+
                    '<div class="mui-clearfix" id="recommendBox'+num+'"></div>'+
                    '</div>').appendTo($('#segmentedControlContents'))

                //热门品牌
                var moreBrand = '';
                //if(num==0||num==1){
                    //moreBrand = '<span class="mui-icon mui-icon-arrowright"></span><span class="more_brand">更多品牌</span>'
                //}
                var threeData = secondData.secondCategory;
                hrefId = threeData[0].id;
                $('<h2 class="category_small_title" style="margin-bottom: 6px;">' +
                    ''+threeData[0].name+moreBrand+'</h2>' +
                    '<ul class="mui-table-view mui-clearfix" id="brandUlBox'+num+'"></ul>').appendTo($('#brandBox'+num+''))
                for(var b=0;b<threeData[0].thirdCategory.length;b++){
                    $('<li class="mui-table-view-cell" id="'+threeData[0].thirdCategory[b].id+'">' +
                        '<img src="'+threeData[0].thirdCategory[b].imgUrl+'" alt="">' +
                        '<span>'+threeData[0].thirdCategory[b].name+'</span>'+
                        '</li>').appendTo($('#brandUlBox'+num+''))
                }
                for(var k = 1;k<threeData.length;k++){
                    //热门推荐
                    $('<h2 class="category_small_title">'+threeData[k].name+'</h2>' +
                        '<div class="mui-clearfix"><ul class="mui-table-view mui-clearfix" id="recommendUlBox'+k+'"></ul></div>').appendTo($('#recommendBox'+num+''))
                    for(var c=0;c<threeData[k].thirdCategory.length;c++){
                        $('<li class="mui-table-view-cell" id="'+threeData[k].thirdCategory[c].id+'">' +
                            '<img src="'+threeData[k].thirdCategory[c].imgUrl+'" alt="">' +
                            '<span>'+threeData[k].thirdCategory[c].name+'</span>'+
                            '</li>').appendTo($('#recommendUlBox'+k+''))
                    }
                }
            }
        })
    }
    //左边点击
    mui('body').on('tap', '.mui-control-item', function(e) {
        $('#segmentedControlContents').empty();
        num = $(this).attr('id');
        $(this).addClass('mui-active');
        $(this).siblings().removeClass('mui-active');
        parentCategoryId = $(this).attr('id');
        doRight();
    });
    //右边点击
    mui('body').on('tap', '.mui-table-view-cell', function(e) {
        var id = $(this).attr('id');
        setItem('cateAct',$('.mui-active').attr('id'));
        window.location.href="goods-list.html?goodsCategory_id="+id;
    });
    //广告图点击
    mui('body').on('tap', '.advertising_img', function(e) {
        window.location.href=$(this).attr('linkUrl');
    });
})

function setStorage(key,value) {

}